<?

//Data loading
$title2='';
if (GetOfficeID()) $perush = GetCompany (GetOfficeID());
else $perush = 'PT. MASAJI PRAYASA CARGO';

$page   = 'PAGE';

$DEFINE_FONT=0;
$pdf=new PDFReport("P","mm","Letter");

$pdf->AddPage();
$Result = GridList();
$pdf->GridTable($Result);
$pdf->Output();

function BuildCondition () {
  $condition = "";
    for ($x=1;$x<=10;$x++) {
      if (GetParam("txtCheck".$x,"")) {
        if (strlen($condition)) $condition .= " OR nobkt='".GetParam("txtCheck".$x,"")."' ";
        else $condition = " nobkt='".GetParam("txtCheck".$x,"")."' ";
      }
    }
  return $condition;
}

function ReadHeaderList () {
     $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
     $SQL = "SELECT * FROM ais_memorial WHERE ".BuildCondition ()." AND urut=999 ORDER BY nobkt desc";
     $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
     if ($MyResult)
        foreach($MyResult as $row){

      }
}

function ReadHeader($nokwit) {

    $TEMP = "TEMPORARY";
    $DBC2 = new mydb(DB_USER, DB_PASSWORD, DB_NAME2, DB_HOST);
    $DBC1 = new mydb(DB_USER, DB_PASSWORD, OfficeDB1, DB_HOST);

  //Dump Vessel & Voyage DB
  /*
    CopyTable("jadwalkapal","ais_voyage",$DBC2,$DBC1,"");
    CopyTable("vessel","ais_vessel",$DBC2,$DBC1,"");
    CopyTable("port","ais_port",$DBC2,$DBC1,"");
    CopyTable("port","ais_portfrm",$DBC2,$DBC1,"");
    CopyTable("port","ais_portto",$DBC2,$DBC1,"");
*/
/*
    $SQL = "
      SELECT ais_memorial.*,DATE_FORMAT(tglbkt,'%d %M, %Y') as spell_date, SUM(jumlah)  as TOTALPAID,
             concat(ifnull(ais_vessel.kodevessel,''),'-',ifnull(ais_vessel.nama,'')) as vessel,
             ifnull( NOVOYAGE,'') as voyage,
             concat(ifnull(ais_port.kodeport,''),'-',ifnull(ais_port.nama,''),'-',ifnull(ais_port.deskripsi,'')) as port,
             concat(ifnull(ais_portfrm.kodeport,''),'-',ifnull(ais_portfrm.nama,''),'-',ifnull(ais_portfrm.deskripsi,'')) as prtfrm,
             concat(ifnull(ais_portto.kodeport,''),'-',ifnull(ais_portto.nama,''),'-',ifnull(ais_portto.deskripsi,'')) as prtto
      FROM ais_memorial
      LEFT JOIN ais_voyage ON ais_memorial.voyage=ais_voyage.novoyage
      LEFT JOIN ais_vessel ON ais_memorial.vessel=ais_vessel.kodevessel
      LEFT JOIN ais_port ON ais_memorial.port=ais_port.kodeport
      LEFT JOIN ais_portfrm ON ais_memorial.prtfrm=ais_portfrm.kodeport
      LEFT JOIN ais_portto ON ais_memorial.prtto=ais_portto.kodeport
      WHERE nobkt='$nokwit'
      GROUP BY nobkt
    ";
*/
  return $DBC1->dbc->get_row($SQL);
}

class PDFReport extends PDF {

    function Header() {
        global $title,$title2,$perush,$page,$DEFINE_FONT,$DBConnection;
        if ($DEFINE_FONT==0) {
            $this->AddFont('Arial','','arialn.php');
            $DEFINE_FONT=1;
        }
        $Field = ReadHeader(GetParam("nobkt",""));


        $this->spell_date = strtoupper($Field->spell_date);
        list($this->year,$this->month,$this->day) = explode("-",$Field->tglbkt);

            $this->SetFont('Arial','',12);
        $this->SetLeftMargin(10);
        $this->Cell(200,7,"",0,1,'L',0);

        $this->Ln();$this->Ln();

        $this->Cell(50,7,"",0,0,'L',0);

        $this->SetFont('Arial','',16);
        $this->Cell(100,7,"",0,0,'C',0);

        $this->SetFont('Arial','',12);
        $this->Cell(50,7,"NO: ".GetParam("nobkt",""),0,1,'R',0);

        $this->Cell(50,7,"",0,0,'L',0);
        $this->SetFont('Arial','',16);
        $this->Cell(100,7,$title,0,0,'C',0);

        $this->SetFont('Arial','',12);
        $this->Cell(50,7,"PAGE: ".$this->PageNo(),0,1,'R',0);
        $this->Cell(200,7,$perush,0,1,'L',0);

//                $this->Cell(150,7,"",0,0,'C',0);
//                $this->Cell(50,7,"PAGE: ",0,1,'R',0);

        $this->Ln();
        $this->SetLeftMargin(10);

                //$this->Line(5,30,210,30);
//                $this->Ln();
                //Header Debit Note

        $this->Cell(35,4,"",0,0,'L',0);
        $this->Cell(50,4,$Field->port,0,0,'L',0);
        $this->Cell(35,4,"",0,0,'L',0);
        $this->Cell(50,4,"TO:",0,1,'L',0);
        $this->Cell(35,4,"",0,0,'L',0);
        $this->Cell(50,4,$Field->debtur,0,0,'L',0);
        $this->Cell(35,4,"",0,0,'L',0);
        $this->Cell(50,4,$Field->debnam,0,1,'L',0);

        $this->Cell(35,4,"",0,0,'L',0);
        $this->Cell(50,4,$Field->vessel." / ".$Field->voyage,0,1,'L',0);
        $this->Cell(35,4,"",0,0,'L',0);
        $this->Cell(50,4,"",0,1,'L',0);
        $this->Cell(35,4,"",0,0,'L',0);
        $this->Cell(50,4,$Field->prtfrm,0,1,'L',0);
        $this->Cell(35,4,"",0,0,'L',0);
        $this->Cell(50,4,$Field->prtto,0,1,'L',0);
                $this->Ln();
        $this->Cell(10,4,"We Debit Your Account",0,0,'L',0);
        $this->Cell(50,4,"",0,1,'L',0);
                $this->Ln();
                $this->Ln();
                $this->Ln();
                $this->Ln();
                $this->Ln();
    }

    function GridTable($MyResult) {
        //Column widths

    $DBC1 = new mydb(DB_USER, DB_PASSWORD, OfficeDB1, DB_HOST);
    $w=array(15,120,20,30);
        //Data
        if ($MyResult)
        foreach($MyResult as $row)
        {
            $Total = $Total + $row["jumlah"];
            $this->Cell($w[0],5,$row["urut"].".",0,0,'R');
            $this->Cell($w[1],5,$row["uraian"],0,0,'L');
          $this->Cell($w[2],5,FormatNumber($row["jumlah"],$DecimalPoint),0,0,'R');
          $this->Cell($w[3],5,$row["skada"],0,0,'C');
            $this->Ln();
            $this->Cell($w[0],5,"",0,0,'R');
            $this->Cell($w[1],5,"            ".$row["uraian_a"],0,0,'L');
          $this->Cell($w[2],5,"",0,0,'R');
          $this->Cell($w[3],5,"",0,0,'C');
            $this->Ln();
            if (strlen($row["uraian_b"])) {
                $this->Cell($w[0],5,"",0,0,'R');
                $this->Cell($w[1],5,"            ".$row["uraian_b"],0,0,'L');
              $this->Cell($w[2],5,"",0,0,'R');
              $this->Cell($w[3],5,"",0,0,'C');
                $this->Ln();
            }

            if (strlen($row["uraian_c"])) {
                $this->Cell($w[0],5,"",0,0,'R');
                $this->Cell($w[1],5,"            ".$row["uraian_c"],0,0,'L');
              $this->Cell($w[2],5,"",0,0,'R');
              $this->Cell($w[3],5,"",0,0,'C');
                $this->Ln();
            }

            if (strlen($row["uraian_d"])) {
                $this->Cell($w[0],5,"",0,0,'R');
                $this->Cell($w[1],5,"            ".$row["uraian_d"],0,0,'L');
              $this->Cell($w[2],5,"",0,0,'R');
              $this->Cell($w[3],5,"",0,0,'C');
                $this->Ln();
            }
        }

      $SQL = "SELECT * FROM ais_memorial ";
        $this->Ln();
        $this->Cell($w[0],5,"999.",0,0,'R');
        $this->Cell($w[1],5,"TOTAL: ",0,0,'L');
      $this->Cell($w[2],5,FormatNumber($Total,$DecimalPoint),0,0,'R');
      $this->Cell($w[3],5,$row["skada"],0,0,'C');
        $this->Ln();

        $this->Ln();
        $cc = new intToRupiah;
        $GetCurrency = $DBC1->dbc->get_var("
        SELECT DISTINCT tusymbol
        FROM tblcurrency, ais_memorial
        WHERE ais_memorial.nobkt='".GetParam("nobkt","")."' AND
              tblcurrency.tusymbol=ais_memorial.curr AND
              ktrasl='".GetOfficeID()."'
        ");
        //$DBConnection->dbc->debug();

        $cc->setTail($GetCurrency);
        $cc->setNumber($Total);
        $SpellNumber = ucfirst(strtoupper(trim($cc->currency)));

        $this->Ln();
      $this->Ln();
        $this->Cell($w[0]+$w[1]+$w[2]+$w[3],5,"Say : ".$SpellNumber,0,0,'L');

        //Closure line
    }

    function Footer() {
        //Closure line
        //$this->Cell(205,0,'','T');
    }

}


?>
